//
//  MainV2ViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/9/27.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MainV2ViewModel.h"

@interface MainV2ViewModel()

@property (nonatomic, strong) ProvinceListData* zoneData;

@property (nonatomic, copy) NSString* zone;

@end

@implementation MainV2ViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    DataManager* dm = [DataManager sharedDataManager];
    dm.isLoadingOver = YES;
    
    self.zoneData = dm.locationPLData;
    
    RACSignal* zoneChangeSignal = [RACObserve(self, zoneData) distinctUntilChanged];
    [[zoneChangeSignal replay] subscribeNext:^(id x)
    {
        @strongify(self);
        
        ProvinceListData* curPListData = x;
        
        if (x == nil)
        {
            self.zone = @"全国";
        }
        else
        {
            self.zone = curPListData.name;
        }
        
        //广播
        [[NSNotificationCenter defaultCenter] postNotificationName:Notify_FirstPageReload object:curPListData];
    }];
    
    self.reqDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        @weakify(self);
        
        NSString* areaID = HCStrValid(self.zoneData.areaID);
        return [[self requestCarousesDataSignal:@"1" area:areaID] doNext:^(id x)
        {
            @strongify(self);
            
            self.adImgsData = x;
        }];
    }];
    
    self.reqLoadingImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        
        DataManager* dm = [DataManager sharedDataManager];
        
        NSString* areaID = HCStrValid(dm.locationPLData.areaID);
        return [[self requestCarousesDataSignal:@"2" area:areaID] flattenMap:^RACStream *(id value)
        {
            BannersData* bannersData = value;
            
            //TODO: 广告图测试
//            BannerData* bannerData = [[BannerData alloc] init];
//            bannerData.image = @"http://img2.3lian.com/img2007/10/24/011.jpg";
//            bannersData.datas = @[bannerData];
            
            NSString* lastLoadingAdImgUrl = [dm lastLoadingAdImgUrl];
            
            if (bannersData.datas.count > 0 && ![lastLoadingAdImgUrl isEqualToString:bannersData.datas[0].image])
            {
                BannerData* bannerData = bannersData.datas[0];
                
                RACSignal* downloadImgSignal = [AFNetWorkUtils racDOWNLOADWthURL:bannerData.image savePath:[dm saveLoadingAdImgFileUrl]];
                return [downloadImgSignal doCompleted:^
                {
                    [dm saveLastLoadingAdImgUrl:bannerData.image];
                    
                    NSLog(@"下载广告完毕");
                }];
            }
            else
            {
                return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
            }
        }];
    }];
}

- (void)resetZoneData:(ProvinceListData*)zoneData
{
    self.zoneData = zoneData;
}

- (RACSignal *)requestCarousesDataSignal:(NSString*)type area:(NSString*)areaID
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        NSDictionary* postData = interface_bannerlist_params(type, areaID);
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_bannerlist params:postData] subscribeNext:^(id x)
         {
             @strongify(self)
             
             [subscriber sendNext:[BannersData modelWithJSON:x]];
             [subscriber sendCompleted];
         }   error:^(NSError *error) {
             //错误处理
             
             [subscriber sendNext:nil];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

@end
